package com.yu.mall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yu.mall.dto.PageDTO;
import com.yu.mall.entity.AdminUser;
import com.yu.mall.entity.Bill;
import com.yu.mall.mapper.BillMapper;
import com.yu.mall.util.JDUtils;
import com.yu.mall.util.ResultUtils;
import com.yu.mall.vo.PageResultVO;
import com.yu.mall.vo.ResultVO;
import com.yu.mall.vo.bill.BillVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("bill")
@Api(tags = "后台账单管理",value = "后台账单管理")
public class BillController {

    @Autowired
    private BillMapper billMapper;

    @GetMapping("list")
    @ApiOperation(value = "查询账单")
    @ApiImplicitParam(name = "date", value = "账单月份",example = "yyyy-MM")
    public PageResultVO<Bill> list(PageDTO dto, String date) throws ParseException {
        Example example = new Example(Bill.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(date)) {
            Date startDate = DateUtils.parseDate(date,"yyyy-MM");
            Date endDate = DateUtils.addMonths(startDate,1);
            criteria.andLessThan("createTime",endDate);
            criteria.andGreaterThanOrEqualTo("createTime",startDate);
        }
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        List<Bill> list = billMapper.selectByExample(example);
        PageInfo<Bill> pageInfo = new PageInfo<>(list);
        return ResultUtils.page(pageInfo);
    }

    @GetMapping("listTotal")
    @ApiOperation(value = "查询账单总计")
    @ApiImplicitParam(name = "date", value = "账单月份",example = "yyyy-MM")
    public ResultVO<BillVO> list(String date) throws ParseException {
        BillVO vo = new BillVO();
        Example example = new Example(Bill.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(date)) {
            Date startDate = DateUtils.parseDate(date,"yyyy-MM");
            Date endDate = DateUtils.addMonths(startDate,1);
            criteria.andLessThan("createTime",endDate);
            criteria.andGreaterThanOrEqualTo("createTime",startDate);
        }
        List<Bill> list = billMapper.selectByExample(example);
        BigDecimal income = new BigDecimal(0);
        BigDecimal expenditure = new BigDecimal(0);
        if(!CollectionUtils.isEmpty(list)){
            for (Bill bill : list) {
                if("1".equals(bill.getType())){
                    income = income.add(bill.getAmount());
                    expenditure = expenditure.add(bill.getJdAmount());
                }else{
                    income = income.subtract(bill.getAmount());
                    expenditure = expenditure.subtract(bill.getJdAmount());
                }

            }
        }
        vo.setIncome(income);
        vo.setExpenditure(expenditure);
        try {
        	String unionBalance = JDUtils.getUnionBalance();
            if(StringUtils.isNotBlank(unionBalance)){
                JSONObject jsonObject = JSON.parseObject(unionBalance);
                JSONObject balance = jsonObject.getJSONObject("balance");
                vo.setUnionBalance(balance.getBigDecimal("remainLimit"));
            }
		} catch (Exception e) {
			// TODO: handle exception
			vo.setUnionBalance(new BigDecimal("0"));
		}
        

        return ResultUtils.success(vo);
    }
}
